package com.mate.cloud.redisson.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@EnableCaching
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(RedisProperties.class)
public class RedissonConfig {


    private final RedisProperties redisProps;

    private final MessageListener messageListener;

    @Bean(name = "redissonClient")
    RedissonClient redissonClient() {
        Config config = new Config();
        config.useClusterServers()        //cluster方式至少6个节点(3主3从，3主做sharding，3从用来保证主宕机后可以高可用)
                .addNodeAddress("redis://192.168.110.65:6379")
                .addNodeAddress("redis://192.168.110.66:6379")
                .addNodeAddress("redis://192.168.110.67:6379")
                .addNodeAddress("redis://192.168.110.65:6380")
                .addNodeAddress("redis://192.168.110.66:6380")
                .addNodeAddress("redis://192.168.110.67:6380");
        return Redisson.create(config);
    }

    @Bean
    public RedissonClient redissonClient1() {
        Config config = new Config();


        // 单机模式
        SingleServerConfig singleServerConfig = config.useSingleServer()
                .setAddress("redis://" + redisProps.getHost() + ":" + redisProps.getPort())
                .setPassword(redisProps.getPassword())  // 无密码可省略
                .setDatabase(0);
        int minIdle = singleServerConfig.getConnectionMinimumIdleSize();
        int poolSize = singleServerConfig.getConnectionPoolSize();

        // 强制保证 poolSize >= minIdle
        if (poolSize < minIdle) {
            poolSize = minIdle; // 若配置值不合法，自动修正
            log.warn("连接池大小小于最小空闲数，已自动修正为: {}", poolSize);
        }
        // 打印参数值（关键！）
        log.info("Redisson 最小空闲连接数: {}", singleServerConfig.getConnectionMinimumIdleSize());
        log.info("Redisson 连接池大小: {}", singleServerConfig.getConnectionPoolSize());
        // 集群模式（示例）
        // config.useClusterServers()
        //       .addNodeAddress("redis://127.0.0.1:7001", "redis://127.0.0.1:7002")
        //       .setPassword("your_password");

        return Redisson.create(config);
    }

    @Bean
    RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        List<Topic> list = new ArrayList<>();
        list.add(new PatternTopic("hui*"));
        list.add(new PatternTopic("test"));
        list.add(new PatternTopic("TOU"));
        container.addMessageListener(messageListener, list);
        return container;
    }
}
